﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMS.MainClasses;
using GMS.ServiceFunctions.Data;

namespace GMS.MenuContent
{
    public partial class AddEditMenuItem : ChildForms
    {
        private String caption;
        BindingSource bsForms = new BindingSource();
        BindingSource bsMenu = new BindingSource();
        public String Caption
        {
            get { return caption; }
            set { caption = value; }
        }
        Int32 idMenu;
        public Int32 IdMenu
        {
            get { return idMenu; }
            set { idMenu = value; }
        }
        public enum EditMode
        { 
            Добавление,
            Редактирование,
            Удаление
        }
        private EditMode formMode;
        public EditMode FormMode
        {
            get { return formMode; }
            set { formMode = value; }
        }

        public AddEditMenuItem(EditMode FormMode)
        {
            InitializeComponent();
            this.FormMode = FormMode;
            StoredProcedures = new String[5];

            StoredProcedures[0] = "[SelectForms]";
            StoredProcedures[1] = "[SelectMenuContent]";
            StoredProcedures[2] = "[AddMenuItem]";
            StoredProcedures[3] = "[DeleteMenuItem]";
            StoredProcedures[4] = "[EditMenuItem]";

            if (FormMode == EditMode.Добавление)
                Text = "Добавление меню";
            if (FormMode == EditMode.Редактирование)
                Text = "Редактирование меню";
            if (FormMode == EditMode.Удаление)
            {
                Text = "Удаление меню";
                textBox1.Enabled = false;
                groupBox1.Enabled = false;
                dataGridView1.Enabled = false;
            }
        }

        private void acceptButton1_Click(object sender, EventArgs e)
        {
            if (FormMode == EditMode.Добавление)
            {
                if (textBox1.Text.Trim() == String.Empty)
                {
                    MessageBox.Show("Введите название меню!!!", "Ошибка!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (dataGridView1.SelectedRows.Count == 0 && IdMenu !=0)
                {
                    MessageBox.Show("Вы не выбрали запись!!!","Предупреждение!!!",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
                    return;
                }
                if (IdMenu != 0)
                    MRF.ExecuteSqlCommand(StoredProcedures[2] + " '" + textBox1.Text.Trim() + "'," + Convert.ToInt32(dataGridView1.CurrentRow.Cells["IdForm"].Value) + ", " + IdMenu, ConnectionString);
                else
                    MRF.ExecuteSqlCommand(StoredProcedures[2] + " '" + textBox1.Text.Trim() + "'", ConnectionString);
            }
            if (FormMode == EditMode.Редактирование)
            {
                if (dataGridView1.SelectedRows.Count == 0)
                    MRF.ExecuteSqlCommand(StoredProcedures[4] + " " + IdMenu + ", '" + textBox1.Text.Trim() + "'", ConnectionString);
                else
                {
                    MRF.ExecuteSqlCommand(StoredProcedures[4] + " " + IdMenu + ", '" + textBox1.Text.Trim() + "'," + Convert.ToInt32(dataGridView1.CurrentRow.Cells["IdForm"].Value), ConnectionString);
                }
            }
            if (FormMode == EditMode.Удаление)
            {
                if (IdMenu == 0)
                {
                    MessageBox.Show("Ветка имеет дочерние элементы. Удаление не возможно!!!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                MRF.ExecuteSqlCommand(StoredProcedures[3] + " " + IdMenu, ConnectionString);
            }
        }
        private void AddEditMenuItem_Load(object sender, EventArgs e)
        {
            bsForms.DataSource = MRF.ReturnDataSetDB(ConnectionString, StoredProcedures[0]);
            bsForms.DataMember = "table";

            dataGridView1.DataSource = bsForms;

            if (FormMode == EditMode.Редактирование || FormMode == EditMode.Удаление)
            {
                bsMenu.DataSource = MRF.ReturnDataSetDB(ConnectionString,StoredProcedures[1]);
                bsMenu.DataMember = "table";

                bsMenu.Filter = "[IdItem] = " + IdMenu;

                textBox1.DataBindings.Add("Text", bsMenu, "CaptionItem");

                if (((DataRowView)bsMenu.List[0])["IdForm"].ToString() != String.Empty)
                    bsForms.Filter = "[IdForm] = " + ((DataRowView)bsMenu.List[0])["IdForm"].ToString();
                else
                {
                    bsForms.Filter = "[IdForm] = -1";
                    dataGridView1.Enabled = false;
                }
            }

        }

        private void textBox4_KeyUp(object sender, KeyEventArgs e)
        {
            bsForms.Filter = "[Название формы] like '%" + textBox4.Text.Trim() + "%'";
        }

        private void textBox3_KeyUp(object sender, KeyEventArgs e)
        {
            bsForms.Filter = "[Сборка] like '%" + textBox3.Text.Trim() + "%'";
        }

        private void textBox5_KeyUp(object sender, KeyEventArgs e)
        {
            bsForms.Filter = "[Пространство имен] like '%" + textBox5.Text.Trim() + "%'";
        }
    }
}
